﻿using System;
using System.Collections.ObjectModel;
using System.Data.Entity;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using Uinside.CreditSystem.DomainModels.Models;
using Uinside.CreditSystem.Tools;

namespace Uinside.CreditSystem.WPF
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        protected override void OnStartup(StartupEventArgs e)
        {
            IntializeData();
            //Background thread to check if there are urgent order
            Task.Run(() =>
            {
                while (true)
                {
                    using (var creditContext = new CreditContext())
                    {
                        var payments = creditContext.Payments;
                        foreach (var payment in payments)
                        {
                            if (payment.RemindDate <= DateTime.Now)
                            {
                                Console.WriteLine(payment.Id + " should be pay");
                            }
                        }
                    }
                    Thread.Sleep(10000);
                }
            });
            base.OnStartup(e);
        }

        private void IntializeData()
        {
            // Forces initialization of database on model changes.
            using (var creditContext = new CreditContext())
            {
                if (creditContext.Database.Exists()) return;

                #region Payment status type Settings

                var paymentTypeStatus1 = new Setting()
                {
                    Type = SettingType.PaymentTypeStatus.GetValue(),
                    Name = "已提交",
                    Value = 1
                };

                var paymentTypeStatus2 = new Setting()
                {
                    Type = SettingType.PaymentTypeStatus.GetValue(),
                    Name = "已付款",
                    Value = 2
                };

                creditContext.Settings.Add(paymentTypeStatus1);
                creditContext.Settings.Add(paymentTypeStatus2);

                #endregion


                #region Banks Settings

                var bank1 = new Setting()
                {
                    Type = SettingType.Bank.GetValue(),
                    Name = "建设银行",
                    Value = 1
                };

                var bank2 = new Setting()
                {
                    Type = SettingType.Bank.GetValue(),
                    Name = "农业银行",
                    Value = 2
                };

                var bank3 = new Setting()
                {
                    Type = SettingType.Bank.GetValue(),
                    Name = "工商银行",
                    Value = 3
                };

                creditContext.Settings.Add(bank1);
                creditContext.Settings.Add(bank2);
                creditContext.Settings.Add(bank3);

                #endregion

                #region Payment type Settings

                var paymentType1 = new Setting()
                {
                    Type = SettingType.PaymentType.GetValue(),
                    Name = "入账",
                    Value = 1
                };

                var paymentType2 = new Setting()
                {
                    Type = SettingType.PaymentType.GetValue(),
                    Name = "出账",
                    Value = 2
                };

                var paymentType3 = new Setting()
                {
                    Type = SettingType.PaymentType.GetValue(),
                    Name = "还息",
                    Value = 3
                };

                var paymentType4 = new Setting()
                {
                    Type = SettingType.PaymentType.GetValue(),
                    Name = "本公司还息",
                    Value = 4
                };


                var paymentType5 = new Setting()
                {
                    Type = SettingType.PaymentType.GetValue(),
                    Name = "打款",
                    Value = 5
                };



                creditContext.Settings.Add(paymentType1);
                creditContext.Settings.Add(paymentType2);
                creditContext.Settings.Add(paymentType3);
                creditContext.Settings.Add(paymentType4);
                creditContext.Settings.Add(paymentType5);


                #endregion

                #region Bank Account Status

                var bankAccountStatus1 = new Setting()
                {
                    Type = SettingType.BankAccountStatus.GetValue(),
                    Name = "正常",
                    Value = 1
                };

                var bankAccountStatus2 = new Setting()
                {
                    Type = SettingType.BankAccountStatus.GetValue(),
                    Name = "锁定",
                    Value = 2
                };
                creditContext.Settings.Add(bankAccountStatus1);
                creditContext.Settings.Add(bankAccountStatus2);

                #endregion

                #region Setting Role

                var adminSetting = new Setting() { Type = 0, Description = "用户角色", Name = "管理员", Value = 1 };
                creditContext.Settings.Add(adminSetting);

                #endregion

                #region Order status


                var status1 = new Setting() { Type = SettingType.OrderStatus.GetValue(), Name = "接受订单", Value = 0 };
                var status2 = new Setting() { Type = SettingType.OrderStatus.GetValue(), Name = "审核通过", Value = 1 };
                var status3 = new Setting() { Type = SettingType.OrderStatus.GetValue(), Name = "已打款", Value = 2 };
                var status4 = new Setting() { Type = SettingType.OrderStatus.GetValue(), Name = "还息期", Value = 3 };
                var status5 = new Setting() { Type = SettingType.OrderStatus.GetValue(), Name = "已关闭", Value = 4 };
                var status6 = new Setting() { Type = SettingType.OrderStatus.GetValue(), Name = "审核失败", Value = 5 };

                creditContext.Settings.Add(status1);
                creditContext.Settings.Add(status2);
                creditContext.Settings.Add(status3);
                creditContext.Settings.Add(status4);
                creditContext.Settings.Add(status5);
                creditContext.Settings.Add(status6);


                #endregion

                #region Initialize Company

                var company = new Customer()
                {
                    Id = 1,
                    CompanyAddress = "重庆开县新城汉丰镇水利家园14-2",
                    CompanyEmail = "denghao_2008@hotmail.com",
                    CompanyName = "Uinside科技有限公司",
                    CompanyPhone = "15902360246",
                    CompanyPortalUrl = "www.Uinside.org",
                    CompanyPostCode = "400054",
                    ContactEmail = "157816168@qq.com",
                    ContactIdNumber = "500234198710310011",
                    ContactName = "邓浩",
                    ContactPhone = "023-52229162",
                    ContactQQ = "157816168",
                    CustomerNumber = "0000001",
                    Capital = 9999.99m
                };
                creditContext.Customers.Add(company);

                #endregion

                #region Admin Employee

                var employee = new Employee()
                {
                    UserName = "Admin",
                    Password = "Admin",
                    Role = adminSetting//new Setting() { Type = 0, Description = "用户角色", Name = "管理员", Value = 1 }
                };
                creditContext.Employees.Add(employee);

                #endregion

                #region Add demo BanksAccount

                var bankCount1 = new BankAccount()
                {
                    Bank = bank1,
                    BankNumber = "4392254189780001",
                    OpeningBank = "重庆开县",
                    BankName = "邓浩",
                    Status = bankAccountStatus1

                };
                creditContext.BankAccounts.Add(bankCount1);



                var bankCount2 = new BankAccount()
                {
                    Bank = bank2,
                    BankNumber = "4392254189780001",
                    OpeningBank = "重庆万州",
                    BankName = "王晶",
                    Status = bankAccountStatus2
                };
                creditContext.BankAccounts.Add(bankCount2);

                if (company.BankAccounts == null)
                    company.BankAccounts = new ObservableCollection<BankAccount>();
                company.BankAccounts.Add(bankCount1);
                company.BankAccounts.Add(bankCount2);

                #endregion

                creditContext.SaveChanges();
            }
        }
    }
}
